package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"sport_booking/models"
	"sport_booking/utils"
	"sport_booking/utils/message"
	"sport_booking/utils/validator"
)

/*
添加活动信息
*/
func AddActivityInfo(c *gin.Context) {
	var activity models.Activity
	var status int
	err := c.ShouldBindJSON(&activity)
	if err != nil {
		message.SendMsg(c, message.ErrorBindData)
		return
	}
	// 验证
	msg, code := validator.Validate(&activity)
	if code != message.SUCCESS {
		c.JSON(http.StatusOK, gin.H{
			"status":  code,
			"message": msg,
		})
		return
	}
	//if activity.Name == "" || activity.Note == "" || activity.Address == "" || activity.StartStamp == 0 || activity.EndStamp == 0 {
	//	message.FailMsg("非法操作")
	//	return
	//}
	//if utils.VerifyStampFormat(activity.StartStamp) == false || utils.VerifyStampFormat(activity.EndStamp) == false {
	//	message.FailMsg("时间戳格式错误")
	//	return
	//}
	//if activity.StartStamp < utils.GetTimeStamp() || activity.EndStamp < utils.GetTimeStamp() {
	//	message.FailMsg("过期时间戳，请检查")
	//	return
	//}
	// 防止重复添加同一活动
	status = models.CheckActivityCount(&activity)
	if status == message.SuccessActivityExist || status == message.ErrorCheckActivity {
		message.SendMsg(c, status)
		return
	}
	activity.CreateStamp = utils.GetTimeStamp()
	activity.UUID = c.GetString("uuid")
	id, status := models.AddActivityInfo(&activity)
	if status != message.SUCCESS {
		message.SendMsg(c, status)
		return
	}
	message.SendMsg(c, message.SUCCESS, id)
}

/*
根据uuid和活动id修改活动信息
*/
func UpdateActivityInfo(c *gin.Context) {
	var activity models.Activity
	var status int
	err := c.ShouldBindJSON(&activity)
	if err != nil {
		message.SendMsg(c, message.ErrorBindData)
		return
	}
	if activity.ID <= 0 {
		message.SendMsg(c, message.ErrorActivityId)
		return
	}
	// 验证
	msg, code := validator.Validate(&activity)
	if code != message.SUCCESS {
		c.JSON(http.StatusOK, gin.H{
			"status":  code,
			"message": msg,
		})
		return
	}
	//if activity.ID == 0 || activity.Name == "" || activity.Note == "" || activity.Address == "" ||
	//	activity.StartStamp == 0 || activity.EndStamp == 0 {
	//	message.FailMsg("非法操作")
	//	return
	//}
	//if utils.VerifyStampFormat(activity.StartStamp) == false || utils.VerifyStampFormat(activity.EndStamp) == false {
	//	message.FailMsg("时间戳格式错误")
	//	return
	//}
	//if activity.StartStamp < utils.GetTimeStamp() || activity.EndStamp < utils.GetTimeStamp() {
	//	message.FailMsg("过期时间戳，请检查")
	//	return
	//}
	status = models.CheckActivityCount(&activity)
	if status == message.SuccessActivityExist || status == message.ErrorCheckActivity {
		message.SendMsg(c, status)
		return
	}
	activity.UpdateStamp = utils.GetTimeStamp()
	activity.User.UUID = c.GetString("uuid")
	status = models.UpdateActivityInfo(&activity)
	if status != message.SUCCESS {
		message.SendMsg(c, status)
		return
	}
	message.SendMsg(c, message.SUCCESS)
}

/*
根据uuid取活动信息列表
*/
func GetActivityInfoByUUID(c *gin.Context) {
	var next models.Next
	err := c.ShouldBindJSON(&next)
	if err != nil {
		message.SendMsg(c, message.ErrorBindData)
		return
	}
	// 验证
	msg, code := validator.Validate(&next)
	if code != message.SUCCESS {
		c.JSON(http.StatusOK, gin.H{
			"status":  code,
			"message": msg,
		})
		return
	}
	//if next.Page < 1 || next.Line < 1 {
	//	message.FailMsg("非法操作")
	//	return
	//}
	activities, status := models.GetActivityInfoListByUUID(c.GetString("uuid"), &next)
	if status != message.SUCCESS {
		message.SendMsg(c, status)
		return
	}
	message.SendMsg(c, message.SUCCESS, activities)
}

///*
//根据uuid取所有活动信息列表
//*/
//func GetActivityAllByUUID(c *gin.Context) {
//	message.SetMessageContext(c)
//	var next models.Next
//	err := c.ShouldBindJSON(&next)
//	if err != nil {
//		message.SendMsg(c, message.ErrorBindData)
//		return
//	}
//	// 验证
//	msg, code := validator.Validate(&next)
//	if code != message.SUCCESS {
//		c.JSON(http.StatusOK, gin.H{
//			"status":  code,
//			"message": msg,
//		})
//		return
//	}
//	//if next.Page < 1 || next.Line < 1 {
//	//	message.FailMsg("非法操作")
//	//	return
//	//}
//	uuid := c.GetString("uuid")
//	activities, status := models.GetActivityInfoAllByUUID(uuid, &next)
//	if status != message.SUCCESS {
//		message.SendMsg(c, message.ErrorGetActivityList)
//	}
//	message.SendMsg(c, message.SuccessGetActivityList, activities)
//}

/*
获取活动信息列表
*/
func GetActivityInfoAll(c *gin.Context) {
	var next models.Next
	err := c.ShouldBindJSON(&next)
	if err != nil {
		message.SendMsg(c, message.ErrorBindData)
		return
	}
	// 验证
	msg, code := validator.Validate(&next)
	if code != message.SUCCESS {
		c.JSON(http.StatusOK, gin.H{
			"status":  code,
			"message": msg,
		})
		return
	}
	activities, status := models.GetActivityInfoList(&next)
	if status != message.SUCCESS {
		message.SendMsg(c, status)
		return
	}
	message.SendMsg(c, message.SUCCESS, activities)
}

/*
获取最新一条活动信息
*/
func GetNewActivityOneInfo(c *gin.Context) {
	activity, status := models.GetNewActivityInfo()
	if status != message.SUCCESS {
		message.SendMsg(c, status)
		return
	}
	message.SendMsg(c, message.SUCCESS, activity)
}

///*
//获取所有活动信息列表
//*/
//func GetActivityAll(c *gin.Context) {
//	message.SetMessageContext(c)
//	var next models.Next
//	err := c.ShouldBindJSON(&next)
//	if err != nil {
//		message.SendMsg(c, message.ErrorBindData)
//		return
//	}
//	if next.Page < 1 || next.Line < 1 {
//		message.FailMsg("非法操作")
//		return
//	}
//	activityInfoList, err := models.GetActivityInfoAll(&next)
//	if err != nil {
//		message.ErrorMsg("获取活动信息失败")
//		return
//	}
//	message.OkContentMsg("获取活动信息成功", activityInfoList)
//}
